package com.snack.shop.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单实体
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_order")
@Schema(description = "订单主表")
public class Order extends BaseEntity {

    @Schema(description = "订单号")
    @TableField("order_no")
    @JsonProperty("orderNo")
    private String orderNo;

    @Schema(description = "用户ID")
    @TableField("user_id")
    @JsonProperty("userId")
    private Long userId;

    @Schema(description = "订单类型：1-零食购买，2-取快递，3-取外卖，4-代取奶咖")
    @TableField("order_type")
    @JsonProperty("orderType")
    private Integer orderType;

    @Schema(description = "订单状态：0-待支付，1-待接单，2-待取件，3-配送中，4-已送达，5-待结算，6-已完成，7-已取消，8-已退款，9-退款审核中")
    private Integer status;

    @Schema(description = "退款状态：0-无退款，1-退款审核中，2-退款成功，3-退款被拒绝")
    @TableField("refund_status")
    @JsonProperty("refundStatus")
    private Integer refundStatus;

    @Schema(description = "实际退款金额（给发布者）")
    @TableField("refund_amount")
    @JsonProperty("refundAmount")
    private BigDecimal refundAmount;

    @Schema(description = "补偿金额（给接单人，记入rider_earning）")
    @TableField("compensation_amount")
    @JsonProperty("compensationAmount")
    private BigDecimal compensationAmount;

    @Schema(description = "退款申请时间")
    @TableField("refund_apply_time")
    @JsonProperty("refundApplyTime")
    private LocalDateTime refundApplyTime;

    @Schema(description = "拒绝退款原因（商家拒绝时填写）")
    @TableField("refund_reject_reason")
    @JsonProperty("refundRejectReason")
    private String refundRejectReason;

    @Schema(description = "订单总金额")
    @TableField("total_amount")
    @JsonProperty("totalAmount")
    private BigDecimal totalAmount;

    @Schema(description = "配送费")
    @TableField("delivery_fee")
    @JsonProperty("deliveryFee")
    private BigDecimal deliveryFee;

    @Schema(description = "实付金额")
    @TableField("actual_amount")
    @JsonProperty("actualAmount")
    private BigDecimal actualAmount;

    @Schema(description = "收货地址ID")
    @TableField("address_id")
    @JsonProperty("addressId")
    private Long addressId;

    @Schema(description = "收货地址详情（冗余）")
    @TableField("delivery_address")
    @JsonProperty("deliveryAddress")
    private String deliveryAddress; 

    @Schema(description = "配送员ID")
    @TableField("delivery_user_id")
    @JsonProperty("deliveryUserId")
    private Long deliveryUserId;

    @Schema(description = "备注")
    private String remark;

    @Schema(description = "取消原因")
    @TableField("cancel_reason")
    @JsonProperty("cancelReason")
    private String cancelReason;

    @Schema(description = "取消时间")
    @TableField("cancel_time")
    @JsonProperty("cancelTime")
    private LocalDateTime cancelTime;

    @Schema(description = "支付方式：2-微信支付")
    @TableField("pay_type")
    @JsonProperty("payType")
    private Integer payType;

    @Schema(description = "支付时间")
    @TableField("pay_time")
    @JsonProperty("payTime")
    private LocalDateTime payTime;

    @Schema(description = "微信支付交易单号")
    @TableField("transaction_id")
    @JsonProperty("transactionId")
    private String transactionId;

    @Schema(description = "微信预支付交易会话标识")
    @TableField("prepay_id")
    @JsonProperty("prepayId")
    private String prepayId;

    @Schema(description = "接单时间")
    @TableField("accept_time")
    @JsonProperty("acceptTime")
    private LocalDateTime acceptTime;

    @Schema(description = "完成时间")
    @TableField("finish_time")
    @JsonProperty("finishTime")
    private LocalDateTime finishTime;

    // ==================== 代取模块新增字段 ====================

    @Schema(description = "结算状态：0-未结算，1-申请中，2-已结算")
    @TableField("settlement_status")
    @JsonProperty("settlementStatus")
    private Integer settlementStatus;

    @Schema(description = "申请结算时间")
    @TableField("settlement_apply_time")
    @JsonProperty("settlementApplyTime")
    private LocalDateTime settlementApplyTime;

    @Schema(description = "结算完成时间")
    @TableField("settlement_time")
    @JsonProperty("settlementTime")
    private LocalDateTime settlementTime;

    @Schema(description = "平台服务费")
    @TableField("platform_fee")
    @JsonProperty("platformFee")
    private BigDecimal platformFee;

    @Schema(description = "平台服务费率（%）")
    @TableField("platform_fee_rate")
    @JsonProperty("platformFeeRate")
    private BigDecimal platformFeeRate;

    @Schema(description = "接单方实得收益")
    @TableField("rider_earning")
    @JsonProperty("riderEarning")
    private BigDecimal riderEarning;

    @Schema(description = "确认收货时间")
    @TableField("confirm_time")
    @JsonProperty("confirmTime")
    private LocalDateTime confirmTime;

    // ==================== 结算收款信息 ====================

    @Schema(description = "收款方式（微信、支付宝、银行卡）")
    @TableField("payment_method")
    @JsonProperty("paymentMethod")
    private String paymentMethod;

    @Schema(description = "收款人姓名")
    @TableField("payment_account")
    @JsonProperty("paymentAccount")
    private String paymentAccount;

    @Schema(description = "收款码图片URL")
    @TableField("payment_qr_code")
    @JsonProperty("paymentQrCode")
    private String paymentQrCode;

    @Schema(description = "结算联系电话")
    @TableField("payment_phone")
    @JsonProperty("paymentPhone")
    private String paymentPhone;

    // ========== 非数据库字段 ==========
    
    @Schema(description = "联系人姓名（来自地址表）")
    @TableField(exist = false)
    @JsonProperty("contactName")
    private String contactName;

    @Schema(description = "联系电话（来自地址表）")
    @TableField(exist = false)
    @JsonProperty("contactPhone")
    private String contactPhone;

    @Schema(description = "订单商品明细")
    @TableField(exist = false)
    @JsonProperty("items")
    private List<OrderItem> items;
}

